﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VLUSM_DataAccess.Repositories 
{
    public class PhienBanGiaoRepository : GenericRepository<MainDataContainer>
    {
        public PhienBanGiaoRepository(string connectionString) : base(connectionString, "MainData") { }
        public PhienBanGiaoRepository(MainDataContainer context) : base(context) { }
        public PhienBanGiao GetSingleByID(int IDFilter)
        {
            var query = this.GetFirst<PhienBanGiao>(d => d.ID == IDFilter);
            return query;
        }

        public PhienBanGiao GetSingleByTen(string TenFilter)
        {
            var query = this.GetFirst<PhienBanGiao>(d => d.TenGoi == TenFilter);
            return query;
        }

        public IEnumerable<PhienBanGiao> GetAll()
        {
            var query = this.GetAll<PhienBanGiao>();
            return query;
        }

        public IEnumerable<PhienBanGiao> GetAllByDonViID(int DonViIDFilter)
        {
            //var query = this.GetAll<PhienBanGiao>().Where(p => p.DonViID == DonViIDFilter);
            var query = this.Find<PhienBanGiao>(p => p.DonVi.ID == DonViIDFilter);
            return query;
        }

        public void Save(PhienBanGiao obj)
        {
            try
            {
                if (obj.ID != 0)
                {
                    this.Attach<PhienBanGiao>(obj);
                }
                else
                {
                    this.Add<PhienBanGiao>(obj);
                }
            }
            catch { }
        }

        public void Remove(PhienBanGiao obj)
        {
            this.Delete<PhienBanGiao>(obj);
        }

        public void Remove(int IDFilter)
        {
            var obj = this.GetSingleOrDefault<PhienBanGiao>(p => p.ID == IDFilter);
            this.Delete<PhienBanGiao>(obj);
        }
    }
}
    

